import java.util.Stack;
class Zcsh {
    public int largestRectangleArea(int[] heights) {
        Stack < Integer > stack = new Stack <Integer> ();
        int maxValue = 0;
        stack.push(-1);
        for (int i = 0; i < heights.length; ++i) {
            while (stack.peek() != -1 && heights[stack.peek()] >= heights[i])
                maxValue = Math.max(maxValue, heights[stack.pop()] * (i - stack.peek() - 1));
            stack.push(i);
        }
        while (stack.peek() != -1)
            maxValue = Math.max(maxValue, heights[stack.pop()] * (heights.length - stack.peek() -1));
        return maxValue;
    }
}